<template>
	<div class="system-dicManager-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="500px">

			<el-card shadow="hover">
				<el-form ref="editDialogFormRef" :model="state.ruleForm" :rules="rules" size="default" label-width="80px">
					<el-row :gutter="35">
						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="用户地址" prop="address">
								<el-input v-model="state.ruleForm.address" placeholder="用户地址" clearable></el-input>
							</el-form-item>
						</el-col>


						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="账户类型" prop="addressType">
								<el-select clearable v-model="state.ruleForm.addressType">
									<el-option v-for="i in state.systemTypeSelect" :key="i.value" :label="i.name"
										:value="i.value">
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>

						<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
							<el-form-item label="状态" prop="status">
								<el-select clearable v-model="state.ruleForm.status">
									<el-option label="可用" :value="10"></el-option>
									<el-option label="禁用" :value="99"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>


			</el-card>

			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>

	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref, watch } from 'vue';







import { useFundApi } from '/@/api/fund/index'


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);


const editDialogFormRef = ref();


const rules = {
	address: [
		{ required: true, message: "请输入账号", trigger: "blur" }
	], addressType: [
		{ required: true, message: "请选择类型", trigger: "blur" }
	], status: [
		{ required: true, message: "请选择状态", trigger: "blur" }
	]
}


const state = reactive({
	row: null as any,
	systemTypeSelect: [] as any,
	ruleForm: {
		address: "",
		addressType: "",
		status: ""
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});



// 打开弹窗
const openDialog = async (type: string, row?: any) => {

	systemAddressType();



	if (type === 'edit') {
		state.row = row;
		state.ruleForm = {
			address: row.address,
			addressType: row.addressType,
			status: row.status
		}

		state.dialog.title = '修改账号';
		state.dialog.submitTxt = '修 改';
	} else {

		state.ruleForm = {
			address: "",
			addressType: "",
			status: ""
		}

		state.dialog.title = '新增账号';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};


const systemAddressType = async () => {
	let listTemp = await useFundApi().systemAddressType();
	state.systemTypeSelect = [];
	listTemp.forEach((e: any) => {
		state.systemTypeSelect.push({
			i18nValue: e.i18nValue,
			name: e.name,
			value: e.value
		})
	});
}



// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};


// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = async () => {


	editDialogFormRef.value.validate(async (valid: any) => {
		if (valid) {
			//检查参数列表
			try {


				let addressTypeI18n, addressTypeName;
				state.systemTypeSelect.forEach((e: any) => {
					if (state.ruleForm.addressType == e.value) {
						addressTypeI18n = e.i18nValue;
						addressTypeName = e.name;
					}
				});


				if (state.dialog.type == 'edit') {
					let obj = Object.assign({}, {}, state.ruleForm) as any;
					obj["id"] = state.row.id;
					obj["addressTypeI18n"] = addressTypeI18n;
					obj["addressTypeName"] = addressTypeName;




					await useFundApi().updateSystemAddress(obj);
				} else {
					let obj = Object.assign({}, {}, state.ruleForm) as any;
					obj["addressTypeI18n"] = addressTypeI18n;
					obj["addressTypeName"] = addressTypeName;
					await useFundApi().addSystemAddress(obj);
				}
				ElMessage.success("操作成功");
				closeDialog(); // 关闭弹窗
				emit('refresh');
			} catch (error: any) {
				ElMessage.error("操作失败请稍后再试");
			}


		} else {
			ElMessage.warning("请检查数据的正确性！");
		}
	})




};




watch(
	() => state.dialog.isShowDialog,
	(newValue) => {
		if (newValue == true) {
		}
	}, { immediate: true }
);




// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style lang="scss">
.el-select {
	width: 100% !important;
}



.system-dicManager-dialog-container {
	.bar_tools {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 10px;
	}

	.el-table__expanded-cell {
		padding: 20px;
	}
}
</style>